package com.dk.controller;

import com.dk.pojo.User;
import com.dk.service.StudentService;
import com.dk.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller

public class UserController {
    @Autowired
    private UserService userService;

    HttpSession session=null;

    @ResponseBody
    @RequestMapping("/zhuce")
    public String save13(User user) throws IOException {

        userService.save(user);
        System.out.println(user);
        return "success";

    }

    @ResponseBody
    @RequestMapping("/findall")
    public List<User> save14() {

        List<User> userList = userService.FindAll();

        return userList;

    }

    @RequestMapping("/login")
    public String save14(String user,String password,HttpServletRequest request) {
        User login = userService.Login(user,password);
        session=request.getSession();
        System.out.println(user+password);
        if (login != null) {
            session.setAttribute("user",user);

            if ("Admin".equals(login.getType())) {
                return "teaindex.html";
            } else {

                return "stuindex.html";
            }

        } else return "new.jsp";

    }
    @ResponseBody
    @RequestMapping("/username")
    public String username(HttpServletRequest request) {
        return (String) request.getSession().getAttribute("user");
    }


    @RequestMapping("/xgmm")
    public String xgmm(String user,String password,String type){
        User user1=new User();
        user1.setUser(user);
        user1.setPassword(password);
        user1.setType(type);
        userService.Xg(user1);
        return "index.html";
    }


}
